package com.didi.gulimall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.didi.gulimall.common.constant.CouponConstant;
import com.didi.gulimall.common.utils.PageUtils;
import com.didi.gulimall.common.utils.Query;
import com.didi.gulimall.common.vo.OrderItemVo;
import com.didi.gulimall.coupon.dao.SpuBoundsDao;
import com.didi.gulimall.coupon.entity.CategoryBoundsEntity;
import com.didi.gulimall.coupon.entity.SpuBoundsEntity;
import com.didi.gulimall.coupon.service.CategoryBoundsService;
import com.didi.gulimall.coupon.service.SpuBoundsService;
import com.didi.gulimall.coupon.vo.BonusPointVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("spuBoundsService")
public class SpuBoundsServiceImpl extends ServiceImpl<SpuBoundsDao, SpuBoundsEntity> implements SpuBoundsService {
    @Autowired
    private CategoryBoundsService categoryBoundsService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpuBoundsEntity> page = this.page(
                new Query<SpuBoundsEntity>().getPage(params),
                new QueryWrapper<SpuBoundsEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void getBonusPoints(OrderItemVo orderItem) {
        QueryWrapper<SpuBoundsEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spu_id", orderItem.getSpuId());
        SpuBoundsEntity spuBoundsEntities = baseMapper.selectOne(queryWrapper);
        if (spuBoundsEntities == null) {
            CategoryBoundsEntity categoryBounds = categoryBoundsService.getOne(new QueryWrapper<CategoryBoundsEntity>().eq("category_id", orderItem.getCatalogId()));
            if (categoryBounds == null) {
                return;
            }
            BonusPointVo bonusPointVo = new BonusPointVo();
            BeanUtils.copyProperties(categoryBounds, bonusPointVo);
            bonusPoints(orderItem, bonusPointVo);
        } else {
            BonusPointVo bonusPointVo = new BonusPointVo();
            BeanUtils.copyProperties(spuBoundsEntities, bonusPointVo);
            bonusPoints(orderItem, bonusPointVo);
        }
    }

    private void bonusPoints(OrderItemVo orderItem, BonusPointVo bonusPointVo) {
//       优惠生效情况[1111（四个状态位，从右到左）;0 - 是否有积分优惠1 - 是否有成长值优惠;2 - 成长积分是否赠送;3 - 有优惠，成长值是否赠送【状态位0：不是，1：是】]
        String work = bonusPointVo.getWork();
        char c = work.charAt(0);
        if (c == CouponConstant.OFFER_EFFECTIVE) {

            orderItem.setIntegration(bonusPointVo.getIntegration());
        }
        c = work.charAt(1);
        if (c == CouponConstant.OFFER_EFFECTIVE) {
            orderItem.setGrowth(bonusPointVo.getGrowth());

        }
        c = work.charAt(2);
        if (c == CouponConstant.OFFER_EFFECTIVE) {
            orderItem.setGiveIntegration(bonusPointVo.getGiveIntegration());
        }
        c = work.charAt(3);
        if (c == CouponConstant.OFFER_EFFECTIVE) {
            orderItem.setGiveGrowth(bonusPointVo.getGiveGrowth());
        }
    }
}